// pages/member/releaseItems.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img1: false,
    img2: false,
    imgUrl1: '../images/upload.png',
    imgUrl2: '../images/upload.png',
    imgUrls1: '',
    imgUrls2: '',
    title: '',
    name: "",
    job_number: "",
    phone: "",
    content: '',
    department: "",
    post: '',
    showFlag: false,
  },
  getData(e) {
    let _this = this;
    switch (e.target.dataset.type) {
      case 'name':
        _this.setData({
          name: e.detail.value
        })
        break;
      case 'job_number':
        _this.setData({
          job_number: e.detail.value
        })
        break;
      case 'phone':
        _this.setData({
          phone: e.detail.value
        })
        break;
      case 'department':
        _this.setData({
          department: e.detail.value
        })
        break;
      case 'post':
        _this.setData({
          post: e.detail.value
        })
        break;
    }
  },
  getCon: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  submissionBtn() {
    if (this.data.name == '') {
      wx.showModal({
        title: '提示',
        content: '请输入您的姓名',
        success: function (res) { }
      })
      return false;
    }
    if (this.data.job_number == '') {
      wx.showModal({
        title: '提示',
        content: '请输入您的工号',
        success: function (res) { }
      })
      return false;
    }
    if (this.data.phone == '') {
      wx.showModal({
        title: '提示',
        content: '请输入您的手机号码',
        success: function (res) { }
      })
      return false;
    }
    if (this.data.department == '') {
      wx.showModal({
        title: '提示',
        content: '请输入您所在公司及部门',
        success: function (res) { }
      })
      return false;
    }
    if (this.data.post == '') {
      wx.showModal({
        title: '提示',
        content: '请输入您的职务',
        success: function (res) { }
      })
      return false;
    }
    if (this.data.content == '') {
      wx.showModal({
        title: '提示',
        content: '请输入内容',
        success: function (res) { }
      })
      return false;
    }
    let imgUrls = [];
    if (this.data.imgUrls1) {
      imgUrls[0] = this.data.imgUrls1
    }
    if (this.data.imgUrls2) {
      imgUrls[1] = this.data.imgUrls2
    }
    let imgUrlss = imgUrls.join(',')
    let _this = this
    let datas = {
      code: "application_site",
      userId: getApp().data.userId,
      personName: _this.data.name,
      workNo: _this.data.job_number,
      phone: _this.data.phone,
      deptName: _this.data.department,
      duties: _this.data.post,
      imgUrl: imgUrlss,
      context: _this.data.content
    }
    wx.request({
      url: getApp().data.servsers + '/membership/add.do',
      data: datas,
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        let data = res.data;
        if (data.status == 'success') {
          wx.showModal({
            title: '提示',
            content: '提交成功',
            success(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
          _this.setData({
            name: ''
          })
          _this.setData({
            job_number: ''
          })
          _this.setData({
            phone: ''
          })
          _this.setData({
            department: ''
          })
          _this.setData({
            post: ''
          })
          wx.request({
            url: getApp().data.servsers + '/user/vipService',
            data: {
              userId: getApp().data.userId
            },
            method: 'POST',
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(res) {
              console.log(res.data)
              let data = res.data;
            }
          })
        }

      }
    })
  },
  chooseImg: function (e) {
    let _this = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        if (e.currentTarget.dataset.type == 'img1') {
          _this.setData({
            img1: true
          })
          _this.setData({
            imgUrl1: res.tempFilePaths
          })
        } else if (e.currentTarget.dataset.type == 'img2') {
          _this.setData({
            img2: true
          })
          _this.setData({
            imgUrl2: res.tempFilePaths
          })
        }
        var tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: getApp().data.servsers + '/imgUpload',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data",
            'accept': 'application/json',
          },
          success: function (res) {
            let data = JSON.parse(res.data)
            if (e.currentTarget.dataset.type == 'img1') {
              _this.setData({
                imgUrls1: data.url
              })
              console.log(_this.data.imgUrls1)
            } else if (e.currentTarget.dataset.type == 'img2') {
              _this.setData({
                imgUrls2: data.url
              })
            }
          },
          fail: function (res) {

          },
        })
      },
    })
  },
  removeImg: function (e) {
    if (e.currentTarget.dataset.type == 'img1') {
      this.setData({
        imgUrl1: '../images/upload.png'
      })
      this.setData({
        img1: false
      })
    } else if (e.currentTarget.dataset.type == 'img2') {
      this.setData({
        imgUrl2: '../images/upload.png'
      })
      this.setData({
        img2: false
      })
    }
  },
})